<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图 -->
    <el-card>
      <el-row>
        <el-col :span="10">
          <el-input placeholder="请输入内容" v-model="searchText" clearable>
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>

          <!-- 数据列表区域 -->
          <el-table :data="orderList" border stripe>
            <el-table-column type="index"> </el-table-column>
            <el-table-column prop="order_number" label="订单编号">
            </el-table-column>
            <el-table-column prop="order_price" label="订单价格">
            </el-table-column>
            <el-table-column label="是否付款">
              <template slot-scope="scope">
                <el-tag
                  type="danger"
                  size="mini"
                  v-if="scope.row.pay_status == '0'"
                  >未付款</el-tag
                >
                <el-tag type="success" size="mini" v-else>已付款</el-tag>
              </template>
            </el-table-column>
            <el-table-column prop="is_send" label="是否发货"> </el-table-column>
            <el-table-column prop="create_time" label="下单时间">
              <template slot-scope="scope">
                {{ scope.row.create_time | dateFormat }}
              </template>
            </el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                <el-button
                  type="primary"
                  icon="el-icon-edit"
                  size="mini"
                  @click="showBox"
                ></el-button>
                <el-button
                  size="mini"
                  type="warning"
                  icon="el-icon-edit"
                  @click="showEditBox(scope.row.order_id)"
                ></el-button>
                <el-button
                  type="success"
                  icon="el-icon-location"
                  size="mini"
                  @click="showProgressBox"
                ></el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>

      <!-- 页码条 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <!-- 修改地址的对话框 -->
    <el-dialog
      title="修改地址"
      :visible.sync="addressVisible"
      width="50%"
      @close="addressDialogClosed"
    >
      <el-form
        :model="addressForm"
        :rules="addressFormRules"
        ref="addressFormRef"
        label-width="100px"
      >
        <el-form-item label="省市区/县" prop="address1">
          <el-cascader
            :options="cityData"
            v-model="addressForm.address1"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="address2">
          <el-input v-model="addressForm.address2"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addressVisible = false">取 消</el-button>
        <el-button type="primary" @click="addressVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>

    <!-- 修改订单状态对话框 -->
    <el-dialog
      title="修改订单状态"
      :visible.sync="editVisible"
      width="50%"
      @close="editDialogClosed"
    >
      <el-form ref="editFormRef" :model="statusForm" label-width="80px">
        <el-form-item label="是否付款">
          <el-select v-model="statusForm.pay_status" placeholder="请选择">
            <el-option
              :label="item.status"
              :value="item.para"
              v-for="(item, i) in isPay"
              :key="i"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否发货">
          <el-select v-model="statusForm.is_send" placeholder="请选择">
            <el-option
              :label="item.status"
              :value="item.para"
              v-for="(item, i) in isSend"
              :key="i"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary" @click="edit">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 物流进度对话框 -->
    <el-dialog title="物流进度" :visible.sync="progressVisible" width="50%">
      <!-- 物流时间 -->
      <!-- <el-timeline>
        <el-timeline-item
          v-for="(activity, index) in progressInfo"
          :key="index"
          :timestamp="activity.time"
        >
          {{ activity.context }}
        </el-timeline-item>
      </el-timeline> -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="progressVisible = false">取 消</el-button>
        <el-button type="primary" @click="progressVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import cityData from './citydata.js'
export default {
  data() {
    return {
      // 请求订单信息对象
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10,
      },
      //总页数
      total: 0,
      //要搜索的内容
      searchText: '',
      //获取到的全部订单数据
      orderList: [],
      //修改地址对话框的显示与隐藏
      addressVisible: false,
      //地址信息
      addressForm: {
        address1: [],
        address2: '',
      },
      //地址信息验证规则
      addressFormRules: {
        address1: [{ required: true, message: '请选择地区', trigger: 'blur' }],
        address2: [
          { required: true, message: '请填写详细地址', trigger: 'blur' },
        ],
      },
      cityData,
      //物流进度对话框的显示与隐藏
      progressVisible: false,
      //物流信息
      progressInfo: [],
      // 订单状态对话框显示与隐藏
      editVisible: false,
      //   订单状态信息
      statusForm: {},
      // 订单状态验证规则
      editFormRules: {},
      isSend: [
        {
          status: '已发货',
          para: '1',
        },
        {
          status: '未发货',
          para: '0',
        },
      ],
      isPay: [
        {
          status: '已付款',
          para: '1',
        },
        {
          status: '未付款',
          para: '0',
        },
      ],
    }
  },
  created() {
    this.getOrderList()
  },
  methods: {
    async getOrderList() {
      const { data: res } = await this.$http.get('orders', {
        params: this.queryInfo,
      })
      if (res.meta.status != 200)
        return this.$message.console.error('获取订单信息失败')
      this.$message.success('获取订单信息成功')
      console.log(res.data)
      this.total = res.data.total
      this.orderList = res.data.goods
    },
    //监听页面显示条数的改变
    handleSizeChange(newSize) {
      this.queryInfo.pagesize = newSize
      this.getOrderList()
    },
    //监听页码的变化
    handleCurrentChange(newPage) {
      this.queryInfo.pagenum = newPage
      this.getOrderList()
    },
    //展示修改地址的对话框
    showBox() {
      this.addressVisible = true
    },
    //关闭地址对话框刷新
    addressDialogClosed() {
      this.$refs.addressFormRef.resetFields()
    },
    // 展示订单修改对话框
    async showEditBox(id) {
      const { data: res } = await this.$http.get('orders/' + id)
      if (res.meta.status != 200) return this.$message.error('获取信息失败')
      this.$message.success('获取信息成功')
      this.statusForm = res.data
      console.log(this.statusForm)
      this.editVisible = true
    },
    //订单状态对话框的关闭刷新
    editDialogClosed() {
      this.$refs.editFormRef.resetFields()
    },
    //提交新的订单状态
    async edit() {
      console.log(this.statusForm)
      const { data: res } = await this.$http.put(
        'orders/' + this.statusForm.order_id,
        {
          is_send: this.statusForm.is_send,
          pay_status: this.statusForm.pay_status,
        }
      )
      if (res.meta.status != 200) return this.$message.error('提交失败')
      this.$message.success('提交成功')
      this.getOrderList()
      this.editVisible = true
    },
    //展示物流进度对话款
    async showProgressBox() {
      const { data: res } = await this.$http.get('/kuaidi/804909574412544580')

      if (res.meta.status !== 200) {
        return this.$message.error('获取物流进度失败！')
      }

      this.progressInfo = res.data

      this.progressVisible = true
      console.log(this.progressInfo)
    },
  },
}
</script>

<style lang="less" scoped>
@import '../../plugins/timeline/timeline.css';
@import '../../plugins/timeline-item/timeline-item.css';
.el-cascader {
  width: 100%;
}
</style>